<?php
/** @package    verysimple::Payment */

/** import supporting libraries */
include_once("PaymentRequest.php");
include_once("PaymentResponse.php");

/**
 * PaymentProcessor is an abstract base class for processing PaymentRequest
 * objects.  The purpose of this API is to allow a common PaymentRequest
 * object to be processed by any class that extends PaymentProcessor
 *
 * @package    verysimple::Payment
 * @author     VerySimple Inc.
 * @copyright  1997-2007 VerySimple, Inc.
 * @license    http://www.gnu.org/licenses/lgpl.html  LGPL
 * @version    2.0
 */
abstract class PaymentProcessor
{
	
	public $Username;
	public $Password;
	
	/**
	* Constructor
	* @param bool $testmode default = false 
	*/
	final function PaymentProcessor($testmode = false, $username = "", $password = "")
	{
		$this->Username = $username;
		$this->Password = $password;
		$this->Init($testmode);
	}
	
	/**
	* Init is called by the base object on construction
	* @param bool $testmode 
	*/
	abstract function Init($testmode);
	
	/**
	* Process a PaymentRequest
	* @param PaymentRequest $req Request object to be processed
	* @return PaymentResponse
	*/
	abstract function Process(PaymentRequest $req);
	
	/**
	* @param string $url url to post
	* @param array $data array of arguments for post request
	* @param bool $verify_cert whether to verify an SSL cert. default = false
	* @param bool $use_cookies whether to store a cookie file. default = false
	*/
	protected function CurlPost ($url, $data, $verify_cert = false, $use_cookies = false) 
	{
		// convert the data array into a url querystring
		$post_data = "";
		$delim = "";
		foreach (array_keys($data) as $key)
		{
			$post_data .= $delim . $key ."=" . $data[$key];
			$delim = "&";
		}

		$agent = "curl_post.1";
		// $header[] = "Accept: text/vnd.wap.wml,*.*";    
		$ch = curl_init($url);
		
		curl_setopt($ch,		CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch,		CURLOPT_VERBOSE, 0); ########### debug
		curl_setopt($ch,	    CURLOPT_USERAGENT, $agent);
		//curl_setopt($ch,	    CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch,		CURLOPT_FOLLOWLOCATION, 1);
		
		if ($use_cookies)
		{
			curl_setopt($ch,		CURLOPT_COOKIEJAR, "cook");
			curl_setopt($ch,		CURLOPT_COOKIEFILE, "cook");
		}
		
		curl_setopt($ch,		CURLOPT_POST, 1);
		curl_setopt($ch,		CURLOPT_POSTFIELDS, $post_data);
		curl_setopt($ch,		CURLOPT_SSL_VERIFYPEER, $verify_cert);
		curl_setopt($ch,		CURLOPT_NOPROGRESS, 1);
		
		$tmp = curl_exec ($ch);
		$error = curl_error($ch);
		
		if ($error != "") {$tmp .= $error;}
		curl_close ($ch);
		
		return $tmp;
	}

}

?>